package actions;

/**
 * 对于域对象的新增、修改等操作以及其他的涉及到表单提交（录入界面）的业务操作， 强烈建议采用Struts
 * 动态校验FormBean来实现基于Validator的Form字段校验、重复提交等必要功能。
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

import service.UserAccountService;
import domain.UserAccount;

/**
 * 编辑用户账号的Action对象.
 * @author zhuweiguo
 */
public class UserAccountUpdateAction extends Action {
    /**
     * 用于用户帐号操作.
     */
    private UserAccountService userAccountService = null;

    /**
     * 创建用户帐号.
     * @param mapping action mapping
     * @param form action form
     * @param request http request
     * @param response http response
     * @return action forword
     * @throws Exception any exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 验证是否是有效的Token,防止重复提交
        if (isTokenValid(request, true)) {
            DynaValidatorForm userAccountForm = (DynaValidatorForm) form;
            String userID = userAccountForm.getString("userID");
            UserAccount newAccount = new UserAccount();
            newAccount.setUserID(userID);
            newAccount.setUserName(userAccountForm.getString("userName"));
            newAccount.setRoleID(userAccountForm.getString("roleID"));
            // 调用UserAccountService修改用户账号
            userAccountService.updateUserAccount(newAccount);
            return mapping.findForward("success");
        } else {
            // 重复提交,转到重复提交失败界面
            return mapping.findForward("error.duplicateSubmit");
        }
    }

    /**
     * @param pUserAccountService The userAccountService to set.
     */
    public void setUserAccountService(UserAccountService pUserAccountService) {
        this.userAccountService = pUserAccountService;
    }

}
